#!/bin/bash
#功能函数
funcs=./functions.sh
if test ! -f $funcs; then
    funcs=`which functions.sh`
    if [ "$funcs" = "" ]; then
        echo "functions.sh not exist!!"
        return
    else
        echo "funcs=$funcs"
	. $funcs
    fi
else
    echo "funcs=$funcs"
    . $funcs  
fi


function showhelp()
{
	echoTextRed "使用说明：
  本脚本必须放在要打patch的工程根目录下，并且要将patch目录拷贝到工程根目录的一新建文件夹中。
  执行示例：$0 -d 77_patch
  参数：
	-d  指定patch目录	：如 -d 77_patch
	-r  指定当前工程svn版本的第二个版本，加快处理结果，如77平台ALPS.ICS2.MP.V1_W_20120703的初始版本为655,第2个版本是657,则使用$0 -d 77_patch -r 657
		如果不指定版本号，刚直接使用diff进行文件的比较
  可以选择是否将patch中的文件都拷贝到工程内。如果在拷贝后想还原，可以重新执行此脚本即可"
	exit
}

function files_extract_by_sequence()
{

	local old_file_name=
	local new_file_name=
	local aaa=
	local patch_tar_files=
	local newfilename=
	local patch_sort=
	local result=
	echo "root=$root"
	echo "patch_directory=$patch_directory"
	echo "platform=$platform"

	aaa=`ls $root/$patch_directory| grep " "`
	echo "aaa=$aaa"
	if [ -n "$aaa" ];then
		cd $root/$patch_directory
		ls | while read tmp_file_name;
		do
			 old_file_name=${tmp_file_name}
			 new_file_name=`echo ${old_file_name} | tr " " "_"`
			 mv "${old_file_name}" "${new_file_name}"
		done
		cd -
	fi

	cp $root/$patch_directory/*/*$platform*.tar.gz /tmp/patch

	patch_tar_files=`ls $patch_file_tmp`


#	echo "patch_tar_files=$patch_tar_files"

	cd $patch_file_tmp
	for i in $patch_tar_files;do
		newfilename=`echo $i | tr "(" "p" | tr ")" "p"`
		mv $i $newfilename
	done
	cd -
	
	patch_sort=`ls $patch_file_tmp | awk -F "_" '{print $6}' | awk -F "-" '{print $1}' | sort -V`
#	echo "patch_sort=$patch_sort"

	echoTextRed "processing................................"
	if ! test -d $patchfilesdirectory; then
		mkdir -pv $patchfilesdirectory
	fi

	for i in $patch_sort;do
		result=`ls $patch_file_tmp | grep $i`
#		echoTextBlue "tar xvf $patch_file_tmp/$result ->>> $patchfilesdirectory/$result"
		mkdir -pv $patchfilesdirectory/$result
		tar xvf $patch_file_tmp/$result -C $patchfilesdirectory/$result >/dev/null
#		echoTextBlue "tar xvf $patch_file_tmp/$result ->>> $patchfilesdirectory/"
		tar xvf $patch_file_tmp/$result -C $patchfilesdirectory/ >/dev/null
	done
	
	
}

function files_stat_check()
{
	local orig_file=
	local project_file=
	local file_num=
	local result=
	local svndiffresult=

	rm -rvf $patch_files_orig $patch_files_project  $patch_files_comparelist $diff_result_file $diff_project_file $same_result_file $modify_file_list $new_result_file

	find ${patchfilesdirectory}/alps/ -type f >$patch_files_orig
	cp $patch_files_orig $patch_files_project
	sed -i "s/\/patch_directory\/alps\//\//" $patch_files_project
	
	echoTextRed "开始进行patch中文件的处理"
	file_num=`cat $patch_files_orig | wc -l`
	echo "文件总个数为 $file_num 个"
	paste $patch_files_orig $patch_files_project >$patch_files_comparelist


#比较patch包中文件和本地工程
	cat $patch_files_comparelist | while read i
	do
		orig_file=`echo $i | awk '{print $1}'`
		project_file=`echo $i | awk '{print $2}'`
		diff -q $orig_file $project_file >/dev/null 2>&1
		result=$?
		if [ "0" = "$result" ];then
			echo "$orig_file" >>$same_result_file 2>&1
		elif [ "1" = "$result" ];then
			echo "$orig_file" >>$diff_result_file 2>&1
			echo "$project_file" >>$diff_project_file 2>&1
		elif [ "2" = "$result" ];then
			echo "$orig_file" >>$new_result_file 2>&1
		fi
	done

#patch包和本地工程有差异的文件里，处理本地工程中对应文件
	cat $diff_project_file | while read i
	do
		echo -e "\n\n\nsvn diff --summarize -r $start_version:HEAD $i">>$modify_file_list
		svn log -r $start_version:HEAD $i >>$modify_file_list 2>&1
	done

}

function files_copy()
{
	local orig_file=
	local project_file=
	echo "files copy............."
	cat $patch_files_comparelist | while read i
	do
		orig_file=`echo $i | awk '{print $1}'`
		project_file=`echo $i | awk '{print $2}'`
		echo "cp -rvpf $orig_file $project_file"
		cp -rvpf $orig_file $project_file
	done
}

function files_revert()
{
	echoTextRed "开始还原拷贝过的文件"
	cat $patch_files_comparelist | while read i
	do
		orig_file=`echo $i | awk '{print $2}'`
		svn revert $orig_file
	done
	echoTextRed "还原完成，程序退出！"
	exit
}

function print_result()
{
	echo "===================================================================================="
	echoTextGreen "patch处理总目录：$patchfilesdirectory"
	echoTextGreen "所有patch文件解压至：$patchfilesdirectory/alps/"
	echoTextGreen "patch中文件的路径：$patch_files_orig"
	echoTextGreen "patch中文件对应本地工程文件的的路径：$patch_files_project"
	echoTextGreen "待比较文件列表:$patch_files_comparelist"
	echoTextGreen "patch文件和本地工程相比相同的文件的列表：$same_result_file"
	echoTextGreen "patch文件和本地工程相比有差异文件的列表：$diff_project_file"
	echoTextGreen "patch文件和本地工程相比新增的文件的列表：$new_result_file"
	echoTextGreen "patch的对应文件在主线上的svn log记录：$modify_file_list"
	echo "===================================================================================="
}

#脚本入口
while test $# != 0
do
    case $1 in
	-d)
	    shift
	    patch_directory=$1
	    ;; 
	-p)
	    shift
	    platform=$1
	    ;;
	-r)
	    shift
	    start_version=$1
	    ;;
	--help | --h)
		shift
		showhelp
		;;
    *)
	   ;;
       esac
       shift
done

patch_file_tmp="/tmp/patch"


root=`pwd`

if [ -f $root/project_info ];then
	start_version=`cat $root/project_info | grep start_version | awk -F '=' '{print $2}'`
	echo "start_version=$start_version"
else
	echoTextRed "当前目录下project_info文件不存在，请提供框架的起始第2个版本号!!!!!!!!!!!!!!"
	showhelp
fi
patchfilesdirectory="$root/patch_directory"
patch_files_orig=$root/patch_directory/patch_files_orig
patch_files_project=$root/patch_directory/patch_files_project

#待比较文件列表
patch_files_comparelist=$root/patch_directory/patch_files_comparelist
#patch包中的文件和工程内文件的差异列表
diff_result_file=$root/patch_directory/diff_result_file
diff_project_file=$root/patch_directory/diff_project_file
#在diff_project_file中主线上被修改过的文件列表
modify_file_list=$root/patch_directory/modify_file_list
#patch包中的文件和工程内无差异的文件列表
same_result_file=$root/patch_directory/same_result_file
#主线上不存在的文件列表
new_result_file=$root/patch_directory/new_result_file

if [ -f $patch_files_comparelist ];then
	result=`question "是否去掉拷贝过去的文件，还原至版本库？" 1 no`
	if [  "$result" = "yes" ] ; then
		files_revert
	fi
fi

rm -rf $patch_file_tmp
mkdir $patch_file_tmp
rm -rf $patchfilesdirectory

echoTextRed "start files_extract......................."
files_extract_by_sequence


echoTextRed "start files_stat_check...................."
files_stat_check


print_result
echoTextRed "Process Compelete............................................"


result=`question "是否用patch中文件覆盖到本地工程？" 1 no`
if [  "$result" = "yes" ] ; then
	echo "开始持贝"
	files_copy
fi
